package com.xky.ui.menu;

import javax.swing.*;
import java.awt.*;
import java.util.function.Consumer;

/**
 * 菜单管理类，处理菜单的创建和管理
 */
public class MenuManager {
    private static JPanel leftPanel;
    private static JPanel currentExpandedPanel;
    
    public MenuManager(JPanel leftPanel) {
        MenuManager.leftPanel = leftPanel;
    }
    
    /**
     * 添加菜单项
     * @param mainMenuText 主菜单文本
     * @param subMenuTexts 子菜单文本数组
     * @param subMenuActions 子菜单动作处理器
     */
    public static void addMenu(String mainMenuText, String[] subMenuTexts, Consumer<String> subMenuActions) {
        // 主菜单按钮
        JButton mainMenuButton = createMenuButton(mainMenuText);

        // 子菜单面板
        JPanel subMenuPanel = new JPanel();
        subMenuPanel.setLayout(new BoxLayout(subMenuPanel, BoxLayout.Y_AXIS));
        subMenuPanel.setBackground(new Color(55, 65, 95));
        subMenuPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
        subMenuPanel.setVisible(false);
        subMenuPanel.setMaximumSize(new Dimension(180, 0));
        subMenuPanel.setAlignmentX(Component.CENTER_ALIGNMENT);

        // 添加子菜单项
        for (String subMenuText : subMenuTexts) {
            JButton subButton = createSubMenuButton(subMenuText);
            subMenuPanel.add(subButton);

            // 子菜单点击事件
            subButton.addActionListener(e -> {
                String actionCommand = e.getActionCommand();
                highlightSelectedButton(subButton);
                subMenuActions.accept(actionCommand);
            });
        }
        
        // 添加主菜单按钮和子菜单面板到左侧面板
        leftPanel.add(mainMenuButton);
        leftPanel.add(subMenuPanel);

        // 添加分割线
        JSeparator menuSeparator = new JSeparator();
        menuSeparator.setMaximumSize(new Dimension(160, 1));
        menuSeparator.setForeground(new Color(70, 80, 110));
        menuSeparator.setAlignmentX(Component.CENTER_ALIGNMENT);
        leftPanel.add(menuSeparator);

        // 主菜单点击事件
        mainMenuButton.addActionListener(e -> {
            if (currentExpandedPanel == subMenuPanel) {
                collapseSubMenu(subMenuPanel);
                currentExpandedPanel = null;
            } else {
                if (currentExpandedPanel != null) {
                    collapseSubMenu(currentExpandedPanel);
                }
                expandSubMenu(subMenuPanel);
                currentExpandedPanel = subMenuPanel;
            }
        });
    }

    /**
     * 创建主菜单按钮
     */
    private static JButton createMenuButton(String text) {
       // 创建一个按钮并设置其文本
JButton button = new JButton(text);
        // 设置按钮的字体为微软雅黑，粗体，大小为16
        button.setFont(new Font("微软雅黑", Font.BOLD, 16));
        // 设置按钮的前景色为白色
        button.setForeground(Color.WHITE);
        // 设置按钮的最大尺寸为180x45
        button.setMaximumSize(new Dimension(180, 45));
        // 设置按钮的首选尺寸为180x45
        button.setPreferredSize(new Dimension(180, 45));
        // 设置按钮在容器中的水平对齐方式为居中
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        // 设置按钮的文本水平位置为左侧
        button.setHorizontalTextPosition(SwingConstants.LEFT);
        // 设置按钮的文本水平对齐方式为左侧
        button.setHorizontalAlignment(SwingConstants.LEFT);
        // 设置按钮不绘制背景
        button.setOpaque(false);
        // 设置按钮的内容区域不填充
        button.setContentAreaFilled(false);
        // 设置按钮不绘制边框
        button.setBorderPainted(false);
        // 设置按钮不绘制焦点边框
        button.setFocusPainted(false);
        // 设置按钮的鼠标悬停时的光标为手型
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        // 设置按钮的边框为空边框，上左下右的边距分别为8, 20, 8, 0
        button.setBorder(BorderFactory.createEmptyBorder(8, 20, 8, 0));

        
        // 添加一个右侧的箭头图标
        button.setText(text + " ▼");
        
        // 添加鼠标悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(new Color(60, 70, 100));
                button.setOpaque(true);
            }
            
            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setOpaque(false);
            }
        });
        
        return button;
    }
    
    /**
     * 创建子菜单按钮
     */
    private static JButton createSubMenuButton(String text) {
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        button.setForeground(new Color(220, 220, 220));
        button.setMaximumSize(new Dimension(180, 35));
        button.setPreferredSize(new Dimension(180, 35));
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        button.setHorizontalTextPosition(SwingConstants.LEFT);
        button.setHorizontalAlignment(SwingConstants.LEFT);
        button.setOpaque(false);          // 使按钮背景透明
        button.setContentAreaFilled(false); // 禁用内容区域自动填充
        button.setBorderPainted(false);    // 不绘制按钮边框
        button.setFocusPainted(false);     // 不显示焦点状态的高亮效果
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        button.setBorder(BorderFactory.createEmptyBorder(5, 30, 5, 0));
        button.setActionCommand(text);
        
        // 添加鼠标悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                if (!button.isSelected()) {
                    button.setBackground(new Color(70, 80, 110));
                    button.setOpaque(true);
                }
            }
            
            public void mouseExited(java.awt.event.MouseEvent evt) {
                if (!button.isSelected()) {
                    button.setOpaque(false);
                }
            }
        });
        
        return button;
    }

    /**
     * 高亮显示选中的按钮
     * @param selectedButton 选中的按钮
     */
    private static void highlightSelectedButton(JButton selectedButton) {
        // 遍历所有子菜单按钮，重置样式
        // 遍历左侧面板中的所有组件
for (Component component : leftPanel.getComponents()) {
            // 检查组件是否为 JPanel 类型
            if (component instanceof JPanel) {
                JPanel panel = (JPanel) component;
                // 遍历 JPanel 中的所有子组件
                for (Component subComp : panel.getComponents()) {
                    // 检查子组件是否为 JButton 类型
                    if (subComp instanceof JButton) {
                        JButton button = (JButton) subComp;
                        // 设置按钮背景为透明
                        button.setBackground(null);
                        // 使按钮不透明
                        button.setOpaque(false);
                        // 取消按钮选中状态
                        button.setSelected(false);
                        // 设置按钮前景色为浅灰色
                        button.setForeground(new Color(220, 220, 220));
                    }
                }
            }

        }
        
        // 设置选中按钮的样式
        selectedButton.setBackground(new Color(41, 128, 185));
        selectedButton.setOpaque(true);
        // 设置选中的按钮为选中状态
        selectedButton.setSelected(true);
        selectedButton.setForeground(Color.WHITE);
    }
    
    /**
     * 展开子菜单
     * @param subMenuPanel 子菜单面板
     */
    private static void expandSubMenu(JPanel subMenuPanel) {
        subMenuPanel.setVisible(true);
        int height = subMenuPanel.getComponentCount() * 35;
        subMenuPanel.setMaximumSize(new Dimension(180, height));

        int index = leftPanel.getComponentZOrder(subMenuPanel) - 1;
        if (index >= 0 && leftPanel.getComponent(index) instanceof JButton) {
            JButton mainButton = (JButton) leftPanel.getComponent(index);
            String text = mainButton.getText().replace("▼", "▲");
            mainButton.setText(text);
            
            // 设置展开菜单的背景色
            mainButton.setBackground(new Color(60, 70, 100));
            mainButton.setOpaque(true);
        }
        
        leftPanel.revalidate();
        leftPanel.repaint();
    }

    /**
     * 折叠子菜单
     * @param subMenuPanel 子菜单面板
     */
    private static void collapseSubMenu(JPanel subMenuPanel) {
        subMenuPanel.setVisible(false);
        subMenuPanel.setMaximumSize(new Dimension(180, 0));

        int index = leftPanel.getComponentZOrder(subMenuPanel) - 1;
        if (index >= 0 && leftPanel.getComponent(index) instanceof JButton) {
            JButton mainButton = (JButton) leftPanel.getComponent(index);
            String text = mainButton.getText().replace("▲", "▼");
            mainButton.setText(text);
            
            // 重置主菜单按钮的背景
            mainButton.setOpaque(false);
        }
        
        leftPanel.revalidate();
        leftPanel.repaint();
    }
} 